package com.lemon.utils.wx;

import java.text.MessageFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

public class TemplateMsgApi {

    private static String sendApiUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={0}";
    private static String H5_SEND_APIURl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=={0}";

    private static final Logger log = LoggerFactory.getLogger(TemplateMsgApi.class);

    /**
     * 发送小程序模板消息
     *
     * @param jsonStr
     * @return {ApiResult}
     */
    private static ApiResult send(String jsonStr, String access_token) {
      
        return send(sendApiUrl, jsonStr,access_token);
    }
    
    /**
     * 发送小程序模板消息模板消息
     * @param jsonStr
     * @return
     */
    private static ApiResult send(String jsonStr) {
       return send(jsonStr, WeiXinUtils.getSmallProgramAuthAccessToken());
    }  
    /**
     * 发送微信模板消息
     * @param jsonStr
     * @param access_token
     * @return
     */
    private static ApiResult send(String apiurl,String jsonStr, String access_token) {
        String url = MessageFormat.format(apiurl,access_token );
        log.info("发送模板参数apiurl={},data={},access_token={}",url, jsonStr,access_token);
        JSONObject jsonResult = HTTPUtil.httpsPost(url, jsonStr);
        log.info("发送模板消息返回，json={}", jsonResult);      
        return JSONObject.toJavaObject(jsonResult, ApiResult.class);
    }

    /**
     * 提现模板消息
     *
     * @param openid
     * @param form_id
     * @param money
     * @return
     */
    public static ApiResult sendExtractMsg(String openid, String form_id, int money) {
        String template_id = "k6y37BB3In5NEx-dk7_0p5jLKw3zJYy6-HULmUXRPo0";
        TemplateData data = TemplateData.newInstance();
        data.setTouser(openid)
                .setForm_id(form_id)
                .setTemplate_id(template_id)
                .add("keyword1", money / 100.0 + "元")
				.add("keyword2" , StringUtil.DateToString(new Date() , "yyyy-MM-dd HH:mm"))
                .add("keyword3", "微信钱包")
                .add("keyword4", "实时到账,请前往微信钱包查看")
                .add("keyword5", "一天最多可提现一次");

        return send(data.build());
    }

    /**
     * 支付成功 模板消息
     *
     * @param openid
     * @param form_id
     * @param money
     * @param orderNo
     * @param course_name
     * @return
     */
    public static ApiResult sendPaySuccessMsg(String openid, String form_id, int money, String orderNo, String course_name, long courseId) {
        String template_id = "nDSnJmY5j7nZDJlboXw8wF1HXI1mGfb5mp3-lZ2ltNM";
        TemplateData data = TemplateData.newInstance();
        data.setTouser(openid)
                .setPage("pages/courseDetails/courseDetails?courseId=" + courseId)
                .setForm_id(form_id)
                .setTemplate_id(template_id)
                .add("keyword1", orderNo)
                .add("keyword2", course_name)
                .add("keyword3", money / 100.0 + "元")
                .add("keyword4", StringUtil.DateToString(new Date(), "yyyy-MM-dd HH:mm"))
                .add("keyword5", "订单支付成功,如有疑问请联系客服");

        return send(data.build());
    }

    /**
     * 发送拼团成功消息
     *
     * @param openid
     * @param form_id     发送formId
     * @param createTime  开团时间
     * @param finshtime   成团时间
     * @param course_name 课程名
     * @param member      成员
     * @return
     */
    public static ApiResult sendGroupSuccessMsg(String openid, String form_id, Date createTime, Date finshtime, String course_name, String member, long courseId) {
        String template_id = "vldhTdR8PAY-Q32ioZqrOvQAzxLXimpNy-saAgad4rA";
        TemplateData data = TemplateData.newInstance();
        data.setTouser(openid)
                .setPage("pages/courseDetails/courseDetails?courseId=" + courseId)
                .setForm_id(form_id)
                .setTemplate_id(template_id)
                .add("keyword1", course_name)
                .add("keyword2", StringUtil.DateToString(createTime, "yyyy-MM-dd HH:mm"))
                .add("keyword3", StringUtil.DateToString(finshtime, "yyyy-MM-dd HH:mm"))
                .add("keyword4", member)
                .add("keyword5", "恭喜你，拼团成功啦");

        return send(data.build());
    }

    /**
     * 发送拼团失败通知
     *
     * @param openid
     * @param form_id
     * @param money       支付金额
     * @param createTime  开团时间
     * @param course_name 课程名
     * @param count       参团人数
     * @return
     */
    public static ApiResult sendGroupFailMsg(String openid, String form_id, int money, Date createTime, String course_name, int count, Long courseId) {
        String template_id = "vldhTdR8PAY-Q32ioZqrOvQAzxLXimpNy-saAgad4rA";
        TemplateData data = TemplateData.newInstance();
        data.setTouser(openid)
                .setForm_id(form_id)
                .setPage("pages/courseDetails/courseDetails?courseId=" + courseId)
                .setTemplate_id(template_id)
                .add("keyword1", course_name)
                .add("keyword2", StringUtil.DateToString(createTime, "yyyy-MM-dd HH:mm"))
                .add("keyword3", money / 100.0 + "元")
                .add("keyword4", "24小时内未凑满" + count + "人参团")
                .add("keyword5", "已支付费用将自动退款至微信零钱");

        return send(data.build());
    }

    /**
     * 发送退款通知
     *
     * @param openid
     * @param form_id
     * @param money
     * @param course_name
     * @return
     */
    public static ApiResult sendRefundsSuccessMsg(String openid, String form_id, int money, String course_name, String success_time) {
        String template_id = "vldhTdR8PAY-Q32ioZqrOvQAzxLXimpNy-saAgad4rA";
        TemplateData data = TemplateData.newInstance();
        data.setTouser(openid)
                .setForm_id(form_id)
                .setTemplate_id(template_id)
                .add("keyword1", course_name)
                .add("keyword2", success_time)
                .add("keyword3", money / 100.0 + "元")
                .add("keyword4", "课程组团失败")
                .add("keyword5", "已完成退款，请注意零钱变动");
        //.add("keyword5", "退款将在1-3个工作日内退还至微信零钱");
        return send(data.build());
    }

    /**
     * 描述: 集call成功，发送消息模板
     *
     * @param openid
     * @param form_id
     * @param mainTitle
     * @param courseId
     * @param perNum
     * @return
     * @auther Ink-足迹
     * @date 2018/7/25 10:24
     */
    public static ApiResult sendCollectCallActivitySuccessMsg(String openid, String form_id, String mainTitle, long courseId, int perNum) {
        String template_id = "VFy6REp9hd3NpzRTXm7buTuXOaz2CdYYtoVIJBvSfMo";
        TemplateData templateData = TemplateData.newInstance();
        templateData.setTouser(openid)
                .setTemplate_id(template_id)
                .setPage(String.format("pages/courseDetails/courseDetails?courseId=%d", courseId))
                .setForm_id(form_id)
                .add("keyword1", mainTitle)
                .add("keyword2", String.format("%d人", perNum))
                .add("keyword3", String.format("已集满%d个好友为你打call", perNum))
                .add("keyword4", "好友助力成功！赶紧进入小程序免费听课吧~");
        return send(templateData.build());
    }
    
    /**
     * 发送h5支付成功
     * @param openid
     * @param form_id
     * @param money
     * @param orderNo
     * @param course_name
     * @param courseId
     * @return
     */
    public static ApiResult sendH5PaySuccessMsg(String openid, String form_id, int money, String orderNo, String course_name, long courseId) {
        String template_id = "STxoKUAzVRAWZn45z9uMqB2HMzD63Dz7xrEZIHJvZ6M";
        TemplateData data = TemplateData.newInstance();
        data.setTouser(openid)
                .setMiniprogramPagepath("pages/courseDetails/courseDetails?courseId=" + courseId)
                .setMiniprogramAppid("wxeae60ffb22f03c28")
                .setTemplate_id(template_id)
                .add("first", "您好，您的课程已购买成功,点击去学习")
                .add("keyword1", orderNo)
                .add("keyword2", StringUtil.DateToString(new Date(), "yyyy-MM-dd HH:mm"))
                .add("keyword3", course_name)
                .add("keyword4", money / 100.0 + "元")
                .add("remark", "订单支付成功,如有疑问请联系客服");

        return send(H5_SEND_APIURl,data.build(), WeiXinUtils.getBaseAccessToken());
    }

}


class ApiResult {

    private int errcode;

    private String errmsg;

    private long msgid;

    public int getErrcode() {
        return errcode;
    }

    public void setErrcode(int errcode) {
        this.errcode = errcode;
    }

    public String getErrmsg() {
        return errmsg;
    }

    public void setErrmsg(String errmsg) {
        this.errmsg = errmsg;
    }

    public long getMsgid() {
        return msgid;
    }

    public void setMsgid(long msgid) {
        this.msgid = msgid;
    }


}
